<?php

namespace app\api\controller\product;

use think\Controller;
use think\Request;

class Cart extends Controller
{
    public function __construct()
    {
        parent::__construct();

        $this->TypeModel = model('Product.Type');
        $this->ProductModel = model('Product.Product');
        $this->UserModel = model('User.User');
        $this->VipModel = model('Product.Vip');
        $this->CartModel = model('Cart');
    }

    //购物车列表
    function index()
    {
        if ($this->request->isPost()) {
            $userid = $this->request->param('userid', 0);
            $cartids = $this->request->param('cartid', 0);

            $user = $this->UserModel->find($userid);

            if (!$userid) {
                $this->error('用户不存在');
                exit;
            }

            $where = [
                'userid' => $userid
            ];

            if (!empty($cartids)) {
                $where['cart.id'] = ['in', $cartids];
            }

            //查询购物车记录
            $cartlist = $this->CartModel->with(['product'])->where($where)->select();

            $this->success('返回购物车记录成功', null, $cartlist);
            exit;
        }
    }

    //添加到购物车
    function add()
    {
        if ($this->request->isPost()) {
            $proid = $this->request->param('proid', 0);
            $userid = $this->request->param('userid', 0);
            $nums = $this->request->param('nums', 0);

            $product = $this->ProductModel->find($proid);

            if (!$product) {
                $this->error('商品不存在');
                exit;
            }

            //判断库存
            if ($product['stock'] <= 0) {
                $this->error('商品库存不足');
                exit;
            }

            $user = $this->UserModel->find($userid);

            if (!$user) {
                $this->error('用户不存在');
                exit;
            }

            $vip = isset($user) ? $user['vip'] : 0;

            if ($vip > 0) {
                $price = $this->VipModel->where(['proid' => $proid, 'level' => $vip])->value('price');
                $price = $price ? $price : $product['price'];
            } else {
                $price = $product['price'];
            }

            //先判断该产品是否有添加到购物车
            $cart = $this->CartModel->where(['userid' => $userid, 'proid' => $proid])->find();

            if ($cart) {
                //更新语句

                //数量
                $nums = $cart['nums'] + 1;

                //总价
                $total = bcmul($nums, $price);

                //组装数据
                $data = [
                    'id' => $cart['id'],
                    'userid' => $userid,
                    'proid' => $proid,
                    'nums' => $nums,
                    'price' => $price,
                    'total' => $total,
                ];

                //更新数据库
                $result = $this->CartModel->validate('common/Cart')->isUpdate(true)->save($data);

                if ($result === FALSE) {
                    $this->error($this->CartModel->getError());
                    exit;
                } else {
                    $this->success('添加购物车成功,是否跳转到购物车立即下单', '/product/cart/index');
                    exit;
                }
            } else {
                //插入语句
                //总价
                $total = bcmul($nums, $price);

                //组装数据
                $data = [
                    'userid' => $userid,
                    'proid' => $proid,
                    'nums' => $nums,
                    'price' => $price,
                    'total' => $total,
                ];

                //插入数据库
                $result = $this->CartModel->validate('common/Cart')->save($data);

                if ($result === FALSE) {
                    $this->error($this->CartModel->getError());
                    exit;
                } else {
                    $this->success('添加购物车成功,是否跳转到购物车立即下单', '/product/cart/index');
                    exit;
                }
            }
        }
    }

    //编辑购物车
    function edit()
    {
        if ($this->request->isPost()) {
            $userid = $this->request->param('userid', 0);
            $cartid = $this->request->param('cartid', 0);
            $nums = $this->request->param('nums', 1);

            $where = [
                'id' => $cartid,
                'userid' => $userid
            ];

            $cart = $this->CartModel->where($where)->find();

            if (!$cart) {
                $this->error('购物车记录不存在');
                exit;
            }

            $user = $this->UserModel->find($userid);

            if (!$user) {
                $this->error('用户不存在');
                exit;
            }

            //重新计算会员的价格
            $vip = isset($user) ? $user['vip'] : 0;

            if ($vip > 0) {
                $price = $this->VipModel->where(['proid' => $cart['proid'], 'level' => $vip])->value('price');
                $price = $price ? $price : $cart['price'];
            } else {
                $price = $cart['price'];
            }

            //总价
            $total = bcmul($nums, $price);

            //组装数据
            $data = [
                'id' => $cartid,
                'nums' => $nums,
                'price' => $price,
                'total' => $total,
            ];

            //执行更新语句
            $result = $this->CartModel->isUpdate(true)->save($data);

            if ($result === FALSE) {
                $this->error($this->CartModel->getError());
                exit;
            } else {
                $this->success('更新购物车成功');
                exit;
            }
        }
    }

    //删除
    function del()
    {
        if ($this->request->isPost()) {
            $userid = $this->request->param('userid', 0);
            $cartid = $this->request->param('cartid', 0);

            $where = [
                'id' => $cartid,
                'userid' => $userid
            ];

            $cart = $this->CartModel->where($where)->find();

            if (!$cart) {
                $this->error('购物车记录不存在');
                exit;
            }

            //删除记录
            $result = $this->CartModel->destroy($cartid);

            if ($result === FALSE) {
                $this->error($this->CartModel->getError());
                exit;
            } else {
                $this->success('删除购物车成功');
                exit;
            }
        }
    }
}
